package com.gss.module.generator.util;

import cn.hutool.core.util.StrUtil;

/**
 * @author DYang
 * @date 2023/6/1
 * 子弹转换
 */
public class CodeTransformUtil {

    /**
     * 把字段转换为大写，以使用下划线进行分割
     * @param value
     * @return
     */
    public static String codeToUpper(String value){
        if (StrUtil.isEmpty(value)) {
            return null;
        }
        String newValue = "";
        int len = value.length();
        for (int i = 0; i < len; i++) {
            char ch = value.charAt(i);
            if (ch == ' ') {
                continue;
            }
            String newVal = String.valueOf(ch);
            newVal = newVal.toUpperCase();
            if (i > 0) {
                char lastCh = value.charAt(i - 1);
                if (isLower(lastCh) && isUpper(ch)) {
                    newVal = "_" + ch;
                }
            }
            newValue += newVal;
        }
        return newValue;
    }

    /**
     * 把字段转换为小写，使用下划线分割
     * @return
     */
    public static String codeToLower(String value){
        value = codeToUpper(value);
        return value != null ? value.toLowerCase() : null;
    }

    /**
     * 字段转换为驼峰形式
     * @param value
     * @return
     */
    public static String codeToHump(String value){
        value = codeToLower(value);
        if (value == null){
            return null;
        }
        String newValue = "";
        int length = value.length();
        for (int i = 0; i < length; i++) {
            char ch = value.charAt(i);
            if (ch == '_') {
                continue;
            }
            String val = String.valueOf(ch);
            if (i > 0 && value.charAt(i - 1) == '_') {
                val = val.toUpperCase();
            }
            newValue += val;
        }
        return newValue;
    }

    /**
     * 判断是否为小写字母
     * @param ch
     * @return
     */
    public static boolean isLower(char ch){
        return ch >= 'a' && ch <= 'z';
    }

    /**
     * 判断是否为小写字母
     * @param ch
     * @return
     */
    public static boolean isUpper(char ch){
        return ch >= 'A' && ch <= 'Z';
    }

}
